
import {deleteById,searchPage,searchColors,searchBrands,searchTypes,saveGift,updateGift} from '../../api/gift'
import pickerOptions from  '@/utils/date';
import {getBase64Str} from '../../utils/base64Utils'
import base64 from '../../utils/getbase64Str2';


let giftInfo = {
    data() {
        return {
            //日期组件
            pickerOptions:pickerOptions,
            //table的list
            tableData: [],
            total:0,
            searchParams:{
                currentPage :1,
                pageSize:10
            },
            colorList: [],
            brandList: [],
            typeList: [],
            dialogVisible:false,
            formDialogVisible:false,
            editDialogVisible:false,
            formData:{},
            editForm:{},
            imageUrl:''
        };
    },
    methods:{
        // indexMethod(index) {
        //     let curpage = this.currentPage; //单前页码，具体看组件取值
        //     let limitpage = this.PageSize; //每页条数，具体是组件取值
        //     return index + 1 + (curpage - 1) * limitpage;
        // },

        //复选框的change事件，删除和修改拿到id
        checkBoxSelectionChange(){

        },
        currentPageChange(currentPage){
            //console.log(currentPage);
            this.searchParams.currentPage = currentPage;
            this.findPage();
        },
      /*  findPage(){
            searchPage(this.searchParams).then(response=>{
               this.total= response.total;
               this.tableData = response.data;
            })
        },  */
      //第二种写法：
       async findPage(){
           let response =  await searchPage(this.searchParams);
               this.total= response.total;
console.log(response.data);
               this.tableData = response.data;
        },
        chooseTime(){
            this.searchParams.startTime=this.pickerOptions.startArrayDate[0];
            this.searchParams.endTime=this.pickerOptions.startArrayDate[1];
        },
        resetForm(){
            this.searchParams={  currentPage :1, pageSize:10};
            this.pickerOptions.startArrayDate ='';
            this.findPage();

        },
        //async表示异步调用 await：表示执行完这条语句，才执行下面语句
       async deleteById(){
           console.log(this.formData.giftId);
           await deleteById(this.formData.giftId);
           this.findPage();
        },
        // getImgStr(e){
        //     console.log("imgstr===");
        //     console.log(e.file);
        //     let fileReader = new FileReader();
        //     fileReader.readAsDataURL(e.file);
        //     fileReader.onload= e=> {
        //         console.log("onload===");
        //         console.log(e);
        //         console.log("图片的base64 e.target.result====");
        //         console.log(e.target.result);
        //         this.formData.brandLogo=e.target.result;
        //         this.imageUrl = e.target.result;
        //
        //     }
        //
        // },
       // getImgStr(e){
       //   getBase64Str(e.file).then(response=>{
       //       this.formData.brandLogo = response;
       //       this.imageUrl =  response;
       //   })
       //  },
        async init() {
            let response1 = await searchColors();
            let response2 = await searchBrands();
            let response3 = await searchTypes();
            this.colorList = response1;
            this.brandList=response2;
            this.typeList=response3;
        },
        getImgStr(e){
            base64.getbase64Str(e.file).then(response=>{
                this.formData.brandLogo=response;
                this.imageUrl = response;
            })

        },
        async addGift(){
            console.log(this.formData.giftTypeid);
            await saveGift(this.formData);
            this.findPage();
        },
        async editGift(){
            await updateGift(this.editForm);
            this.findPage();
        },
        async getThisGift(row){
            this.editForm=JSON.parse(JSON.stringify(row));
            console.log(this.editForm);
        }
    },
    created(){
        this.findPage();
        this.init();

    }
};

export default giftInfo;
